#!/usr/bin/env python3
import re
import sys

def extract_declarations(source_file):
    """从源文件中提取所有 ARM_CMD 宏生成的函数声明"""
    pattern = r'ARM_CMD\(.*?,\s*(\w+)\)'
    declarations = []
    funcs = []
    
    with open(source_file, 'r') as f:
        content = f.read()
        matches = re.findall(pattern, content)
        
        funcs.append("int (*uartCmdFuncs[])(int, char const **) = {")
        for cmd_name in matches:
            decl = f"int adSDKCmd_{cmd_name}(int argc, char const *argv[]);"
            declarations.append(decl)
            funcname = f"    adSDKCmd_{cmd_name},"
            funcs.append(funcname)
        funcs.append("};")
    
    return declarations, funcs

def generate_header(declarations, funcs, output_file):
    """生成头文件内容"""
    header_content = f"""\
// 自动生成的头文件 - 请勿手动修改
#ifndef __AUTO_GEN_UART_DECLS_H__
#define __AUTO_GEN_UART_DECLS_H__

#ifdef __cplusplus
extern "C" {{
#endif

{chr(10).join(declarations)}


{chr(10).join(funcs)}

#ifdef __cplusplus
}}
#endif

#endif // __AUTO_GEN_UART_DECLS_H__
"""
    with open(output_file, 'w') as f:
        f.write(header_content)

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: generate_decls.py <input_source> <output_header>")
        sys.exit(1)
        
    input_file = sys.argv[1]
    output_file = sys.argv[2]
    
    decls, funcs = extract_declarations(input_file)
    generate_header(decls, funcs, output_file)
    print(f"Generated {len(decls)} declarations in {output_file}")



